package com.m3839.sdk.common.util;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;

/**
 * @author ChenBaoyang
 * @description: SpanUtils
 * @date 2021/12/8 11:25
 */
public class SpanUtils {


    /**
     * 处理a标签链接的颜色
     * @param spanned 富文本内容
     * @param color 超链接颜色
     * @return
     */
    public static SpannableStringBuilder getHtmlUrlSpanStyle(Spanned spanned, int color) {
        SpannableStringBuilder htmlBuilder = new SpannableStringBuilder(spanned);
        URLSpan[] urls = htmlBuilder.getSpans(0,
                spanned.length(), URLSpan.class);

        for (final URLSpan urlSpan : urls) {
            int start = htmlBuilder.getSpanStart(urlSpan);
            int end = htmlBuilder.getSpanEnd(urlSpan);
            htmlBuilder.setSpan(new ForegroundColorSpan(color) {
                                    @Override
                                    public void updateDrawState(TextPaint textPaint) {
                                        super.updateDrawState(textPaint);
                                        //去掉下划线，真正有效的地方
                                        textPaint.setUnderlineText(false);
                                    }
                                }, start, end,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }

        return htmlBuilder;

    }

    /**
     * 返回某段字符串中的部门内容，设置高亮颜色和点击事件
     *
     * @param content 字符串内容
     * @param targetContent 要高亮和点击的字符串
     * @param color 要设置的颜色
     * @param listener 点击事件
     * @return
     */
    public static SpannableString getClickableSpannableString(String content, String targetContent,
                                                              int color, View.OnClickListener listener) {
        int start = content.indexOf(targetContent);
        int end = start + targetContent.length();
        SpannableString spannableInfo = new SpannableString(content);
        spannableInfo.setSpan(getClickableSpan(listener, color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableInfo;
    }

    /**
     * 返回通用的带有颜色的可点击的富文本
     *
     * @param listener 点击事件
     * @param color 设置的颜色
     * @return
     */
    public static ClickableSpan getClickableSpan(View.OnClickListener listener, int color) {
        return new ColorClickableSpan(listener, color);
    }

    static class ColorClickableSpan extends ClickableSpan {
        private final View.OnClickListener listener;
        private int color;

        public ColorClickableSpan(View.OnClickListener listener, int color) {
            this.listener = listener;
            this.color = color;
        }

        @Override
        public void onClick(View view) {
            if(listener != null) {
                listener.onClick(view);
            }

        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(color);
            ds.setUnderlineText(false);
        }
    }
}
